package provider

import (
	"time"
	"github.com/kingwrcy/devNews/model"
	"net/http"
	"crypto/sha1"
	"fmt"
	"log"
	"github.com/PuerkitoBio/goquery"
)

type TouTiaoProvider struct{}

func (p *TouTiaoProvider) Schedule() time.Duration {
	return time.Minute * 10
}

func (p *TouTiaoProvider) Name() string{
	return "touTiao"
}

func (p *TouTiaoProvider) GetData(t time.Time) []model.News {
	var news []model.News
	url := "https://toutiao.io/"
	resp, err := http.Get(url)
	if err != nil {
		log.Printf("get %s error : %s", url, err)
		return nil
	}
	defer resp.Body.Close()

	resp, err = http.Get(url)
	if err != nil {
		log.Printf("get %s error : %s", url, err)
		return nil
	}
	defer resp.Body.Close()
	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Printf("NewDocumentFromReader error : %s", err)
		return nil
	}

	 doc.Find("h3.title").Each(func(i int, s *goquery.Selection) {
		 first := s.Find("a").First()
		 href, exists := first.Attr("href")
		 if exists {
			 href = "https://toutiao.io"+href
			 h := sha1.New()
			 h.Write([]byte(fmt.Sprintf("%s%s", first.Text(), href)))
			 uuid := fmt.Sprintf("%x", h.Sum(nil))
			 n := model.News{
				 Title:   first.Text(),
				 Created: time.Now(),
				 Url:     href,
				 Source:  TouTiao,
				 Uuid:    uuid,
			 }
			 news = append(news, n)
		 }
	 })
	return news
}
